What is near-api-js?
The near-api-js package is a JavaScript library for interacting with the NEAR blockchain. It provides tools for developers to build decentralized applications (dApps) on the NEAR platform, manage accounts, deploy smart contracts, and perform transactions.
What are near-api-js's main functionalities?
Account Management
This code demonstrates how to create and manage an account on the NEAR blockchain using near-api-js. It involves setting up a key store, generating a key pair, and connecting to the NEAR testnet.
const nearAPI = require('near-api-js');
const { connect, KeyPair, keyStores } = nearAPI;
async function createAccount() {
const keyStore = new keyStores.InMemoryKeyStore();
const keyPair = KeyPair.fromRandom('ed25519');
await keyStore.setKey('testnet', 'example-account.testnet', keyPair);
const near = await connect({
networkId: 'testnet',
keyStore,
nodeUrl: 'https://rpc.testnet.near.org',
walletUrl: 'https://wallet.testnet.near.org'
});
const account = await near.account('example-account.testnet');
console.log(account);
}
createAccount();
Smart Contract Deployment
This code sample shows how to deploy a smart contract to the NEAR blockchain using near-api-js. It reads a compiled WebAssembly (WASM) file and deploys it to a specified account.
const nearAPI = require('near-api-js');
const { connect, keyStores } = nearAPI;
const fs = require('fs');
async function deployContract() {
const keyStore = new keyStores.InMemoryKeyStore();
const near = await connect({
networkId: 'testnet',
keyStore,
nodeUrl: 'https://rpc.testnet.near.org'
});
const account = await near.account('example-account.testnet');
const contractData = fs.readFileSync('./path-to-wasm-file.wasm');
await account.deployContract(contractData);
console.log('Contract deployed!');
}
deployContract();
Transaction Handling
This example demonstrates how to send a transaction on the NEAR blockchain using near-api-js. It involves sending a specified amount of NEAR tokens from one account to another.
const nearAPI = require('near-api-js');
const { connect, keyStores } = nearAPI;
async function sendTransaction() {
const keyStore = new keyStores.InMemoryKeyStore();
const near = await connect({
networkId: 'testnet',
keyStore,
nodeUrl: 'https://rpc.testnet.near.org'
});
const account = await near.account('example-account.testnet');
const result = await account.sendMoney('receiver-account.testnet', '1000000000000000000000000');
console.log('Transaction result:', result);
}
sendTransaction();
Other packages similar to near-api-js
ethers
Ethers.js is a library for interacting with the Ethereum blockchain. Like near-api-js, it provides tools for managing accounts, deploying contracts, and handling transactions. However, it is specific to Ethereum and its ecosystem, whereas near-api-js is tailored for the NEAR blockchain.
web3
Web3.js is another popular library for interacting with the Ethereum blockchain. It offers similar functionalities to near-api-js, such as account management and contract interaction, but is designed for Ethereum. Web3.js is widely used in the Ethereum community, while near-api-js is specific to NEAR.
near-api-js
A JavaScript/TypeScript library for development of DApps on the NEAR platform
Documentation
Read the TypeDoc API documentation
Examples
(Cheat sheet / quick reference)
(Common use cases / more complex examples)
Contribute to this library
-
Install dependencies
yarn
-
Run continuous build with:
yarn build -- -w
Publish
Prepare dist
version by running:
yarn dist
When publishing to npm use np.
Integration Test
Start the node by following instructions from nearcore, then
yarn test
Tests use sample contract from near-hello
npm package, see https://github.com/nearprotocol/near-hello
Update error schema
Follow next steps:
- Change hash for the commit with errors in the nearcore
- Fetch new schema:
node fetch_error_schema.js
yarn build
to update lib/**.js
files
License
This repository is distributed under the terms of both the MIT license and the Apache License (Version 2.0).
See LICENSE and LICENSE-APACHE for details.